const assert = require('assert');
const errrule = require("../tools/Err");
module.exports = class extends think.Controller
{
    static get _REST()
    {
        return true;
    }

    constructor(ctx)
    {
        super(ctx);
        this.resource = this.getResource();
        this._guid = this.getGuid();
        this.errInstance = new errrule();
        assert(think.isFunction(this.model), 'this.model must be a function');
        this.modelInstance = this.model(this.resource);
    }

    __before()
    {
    }

    /**
     * get resource
     * @return {String} [resource name]
     */
    getResource()
    {
        return this.ctx.controller;
    }

    getGuid()
    {
        const _guid = this.get('guid');
        if (_guid && think.isString(_guid))
        {
            return _guid;
        } else
        {
            return '';
        }
        // const last = this.ctx.path.split('/').slice(-1)[0];
        // if (last !== this.resource) {
        //   return last;
        // }

    }

    async getAction()
    {

        // let data;
        // if (this._guid)
        // {
        //     const pk = this.modelInstance.pk;
        //     data = await this.modelInstance.where({[pk]: this._guid}).find();
        //     return this.success(data);
        // }
        // data = await this.modelInstance.select();
        // return this.success(data);
    }

    /**
     * put resource
     * @return {Promise} []
     */
    async postAction()
    {
        // const pk = this.modelInstance.pk;
        // const data = this.post();
        // delete data[pk];
        // if (think.isEmpty(data))
        // {
        //     return this.fail('data is empty');
        // }
        // const insertId = await this.modelInstance.add(data);
        // return this.success({_guid: insertId});
    }

    /**
     * delete resource
     * @return {Promise} []
     */
    async deleteAction()
    {
        // if (!this._guid)
        // {
        //     return this.fail('params error');
        // }
        // const pk = this.modelInstance.pk;
        // const rows = await this.modelInstance.where({[pk]: this._guid}).delete();
        // return this.success({affectedRows: rows});
    }

    /**
     * update resource
     * @return {Promise} []
     */
    async putAction()
    {
        // if (!this._guid)
        // {
        //     return this.fail('params error');
        // }
        // const pk = this.modelInstance.pk;
        // const data = this.post();
        // delete data[pk];
        // if (think.isEmpty(data))
        // {
        //     return this.fail('data is empty');
        // }
        // const rows = await this.modelInstance.where({[pk]: this._guid}).update(data);
        // return this.success({affectedRows: rows});
    }

    __call()
    {
    }

    //总错误提示方法
    error(errNo, errMsg)
    {
        this.fail(errNo, errMsg);
        return false;
    }


    //成功提示方法
    end(data)
    {
        if (think.isEmpty(data))
        {
            this.ctx.fail(500, '暂无数据');
            return false;
        }
        let resData = data ? data : 0;
        return this.success(data);
    }

    /**
     * 统一错误代码返回方式
     * @param errno
     * @param data
     * @returns {*}
     */
    info(errno, data = '')
    {
        let err = new errrule();
        let status = err.info(errno, data);
        return this.error(status);
    }

    /**
     * 数据库更新语句的返回值
     * @param res
     */
    infoDbUpdate(res)
    {
        let err = new errrule();
        if (res > 0)
        {
            return this.error(err.info(0));
        } else if (res === 0)
        {
            return this.error(err.info(1));
        } else
        {
            return this.error(err.info(-1));
        }

    }


    responseDelete(res)
    {


        let err = new errrule();
        if (res > 0)
        {
            return this.error(err.info(0));
        } else
        {
            return this.responseInfo(20002);
        }
    }

    /**
     * fastvalidate响应统一处理函数
     * @param ValidateResMsg 验证后显示的错误信息
     * @returns {*}
     */
    infoFastValidate(ValidateResMsg)
    {
        return this.fail(this.errInstance.infoFastValidate(ValidateResMsg));
    }

    /**
     * 查询数据统一返回方法
     */
    responseQuery(data)
    {
        if (think.isEmpty(data))
        {
            return this.responseEmpty();
        } else
        {
            return this.fail(this.errInstance.infoQuerySuccess(data));
        }
    }

    /**
     * 空数据返回方法
     * @returns {*}
     */
    responseEmpty()
    {
        return this.fail(this.errInstance.infoEmpty());
    }


    resopnseUpdate(res)
    {
        if (res > 0)
        {
            return this.error(this.errInstance.info(0));
        } else if (res === 0)
        {
            return this.error(this.errInstance.info(1));
        } else
        {
            return this.error(this.errInstance.info(-1));
        }
    }

    responseAdd(res)
    {
        return this.responseOk();
    }


    responseOk()
    {
        return this.error(this.errInstance.info(0));
    }

    responseInfo(errno)
    {
        return this.error(this.errInstance.info(errno));
    }

    responseErrmsg(errmsg,data=''){
        return this.error(this.errInstance.infoWidthErrmsg('888',errmsg,data));
    }

};